﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BySoft.Server.Business;
using BySoft.Server.Common.Entity;
using BySoft.Web.Utility;
using BySoft.Server.Common;
using BySoft.Server.Common.Enum;
using OneSoft.EntityCore;

namespace BySoft.Web.Site
{
    public partial class Login : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ClsComm.ClearDataBase();
            }
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            //string strFactoryType = ddlFactoryType.SelectedValue.Trim();
            string sLogin = "";
           // ClsComm.SetDataBaseType(strFactoryType);
            SysUserEntity oSysUser = new SysUserEntity();
            string sUser = txtUserName.Value.Trim().Replace("'", "");

            sLogin = UserLogin.Login(ref sUser, txtPwd.Value.Trim());
            string b = "";
            if (Session["ValidateCode"] != null)
            {
                b = Session["ValidateCode"].ToString();
            }

            this.Label1.Text = "成功";

            if (sLogin.Equals("ok") && Label1.Text == "成功")
            {
                oSysUser = SysUser.GetUserLoginInfo(sUser);
                SysLoginlogEntity oSysLoginlog = new SysLoginlogEntity();
                oSysLoginlog.UserID = oSysUser.UserID;
                oSysLoginlog.UserName = oSysUser.UserName;
                oSysLoginlog.LogInTime = DateTime.Now;
                oSysLoginlog.LogOutTime = null;
                oSysLoginlog.IPAddress = UIHelper.GetIP();
                oSysLoginlog.LoginResult = "1";
                UserLogin.ChangeLoginInfo(oSysLoginlog);

                
                AddLoginUser(oSysUser);
                //AddLoginFactoryType(strFactoryType);
                
                Response.Redirect("Default.aspx");
            }
            else
            {
                SysLoginlogEntity oSysLoginlog = new SysLoginlogEntity();
                oSysLoginlog.UserID = txtUserName.Value;
                oSysLoginlog.UserName = txtUserName.Value;
                oSysLoginlog.LogInTime = DateTime.Now;
                oSysLoginlog.LogOutTime = null;
                oSysLoginlog.IPAddress = UIHelper.GetIP();
                oSysLoginlog.LoginResult = "0";

                SysLoginfaillogEntity oSysLoginfaillog = new SysLoginfaillogEntity();
                oSysLoginfaillog.IPAddress = UIHelper.GetIP();
                oSysLoginfaillog.LoginTime = DateTime.Now;
                oSysLoginfaillog.UserID = txtUserName.Value;
                oSysLoginfaillog.Password = CommonMethod.Encrypt(txtPwd.Value);

                UserLogin.ChangeLoginFailInfo(oSysLoginlog, oSysLoginfaillog);

                if (sLogin.Equals("ok") && Label1.Text == "失败")
                {
                    sLogin = "验证码错误";
                }

                UIHelper.AlertMessage(this.Page, sLogin);
                return;
            }

        }
    }
}
